package com.toaa.crazymath;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;

import com.startapp.android.publish.StartAppAd.AdMode;
import com.startapp.android.publish.StartAppSDK;
import com.toaa.crazymath.utils.CommonUtils;
import com.vn.toaa.brainracer.R;


public class MainActivity extends BaseAdsActivity {
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		
		// handle case that user pressed OPEN button after installing app
		if (!isTaskRoot()) {
			final Intent intent = getIntent();
			final String intentAction = intent.getAction();
			if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && intentAction != null
					&& intentAction.equals(Intent.ACTION_MAIN)) {
				finish();
				return;
			}
		}

		StartAppSDK.init(this, Constant.STARTAPP_PUBLISHER_ID, Constant.STARTAPP_APP_ID, true);		
		AlarmReceiver.setAlarm(this);
		setContentView(R.layout.main_activity);
		
		if (!isSignedIn()) {
			// sign in Google account
			beginUserInitiatedSignIn();
		}
		
		// show ads
		showExitAdWhenBack();
		new Handler().postDelayed(new Runnable() {
			public void run() {
				showInterstitialAd(AdMode.AUTOMATIC);
			}
		}, 1000);
		
		getSupportFragmentManager().beginTransaction()
				.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out)
				.add(R.id.framelayout_container, new MenuFragment(), Constant.FRAGMENT_TAG_MENU).commit();
	}
	
	@Override
	protected void onPause() {
		if (isFinishing()) {
			AlarmReceiver.cancelAlarm(this);
		}
		super.onPause();
	}
	
	@Override
	public void onBackPressed() {
		final Fragment currentFragment = CommonUtils.getVisibleFragment(this);
		if (currentFragment != null && currentFragment instanceof PlayFragment) {
			getSupportFragmentManager().beginTransaction()
					.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out)
					.replace(R.id.framelayout_container, new MenuFragment()).commit();
		} else {
			displayExitDialog();
		}
	}
	
	/**
	 * 
	 */
	private void displayExitDialog() {
		final Builder builder = new Builder(this);
		builder.setTitle("Exit Confirmation");
		builder.setIcon(android.R.drawable.ic_dialog_info);
		builder.setMessage(R.string.exit_message);
		builder.setPositiveButton("Rate it", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				try {
					Intent intent = new Intent(Intent.ACTION_VIEW);
					String uriString = String.format("market://details?id=%s", getPackageName());
					intent.setData(Uri.parse(uriString));
					startActivity(intent);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		builder.setNegativeButton("Exit", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				finish();
			}
		});
		builder.create().show();
	}
	
}
